package com.agua.enturno.persistencia.dao.impl;

import com.agua.enturno.persistencia.dao.GenericDAO;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.io.Serializable;
import java.util.List;

@SuppressWarnings("unchecked")
public abstract class GenericHibernateDAO<T, ID extends Serializable> 
	extends HibernateDaoSupport implements GenericDAO<T, ID> {

	private Class<T> persistentClass;
	
	/*------------------------------------ Constructors ------------------------------------*/

    protected GenericHibernateDAO(Class<T> persistentClass) {
		this.persistentClass = persistentClass;
	}

	/*--------------------------------------------------------------------------------------*/
    /*------------------------------------ Public methods ----------------------------------*/

    @Override
    public T findById(ID id) {
        return (T) getHibernateTemplate().get(getPersistentClass(), id);
    }
    
    @Override
	public List<T> findByExample(T exampleEntity) {
        return getHibernateTemplate().findByExample(exampleEntity);
    }
    
    @Override
    public List<T> findByCriteria(DetachedCriteria criteria) {
    	return getHibernateTemplate().findByCriteria(criteria);
    }

    @Override
	public List<T> findAll() {
        return getHibernateTemplate().loadAll(getPersistentClass());
    }
    
    @Override
    public T saveOrUpdate(T entity) {
    	getHibernateTemplate().saveOrUpdate(entity);
        return entity;
    }

    @Override
    public void delete(T entity) {
    	getHibernateTemplate().delete(entity);
    }
    
    @Override
    public void flushSession() {
    	getHibernateTemplate().flush();
    }
    
    @Override
    public void clearSession() {
    	getHibernateTemplate().clear();
    }
    
    public Class<T> getPersistentClass() {
        return persistentClass;
    }

    /*--------------------------------------------------------------------------------------*/
    
}

